﻿/*
 *  Copyright (c) 2010, Steven Wang
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *      
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  
 *  twitterSina at http://twitterSina.appspot.com
 *  twitterSina code at http://twitterSina.googlecode.com
 * 	
 */
package twitterSina.servlet.cron;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.datastore.Text;
import twitter4j.Status;
import twitterSina.TwitterAPI;
import twitterSina.common.DateHelp;
import twitterSina.common.DesEncrypt;
import twitterSina.common.SysLogType;
import twitterSina.persistence.SysLog;
import twitterSina.persistence.SysLogDao;
import twitterSina.persistence.TwitterNotify;
import twitterSina.persistence.TwitterNotifyDao;
import twitterSina.robot.RobotMsg;
import java.util.List;

/**
 * Twitter消息提示的Servlet
 * @author Steven Wang <http://blog.stevenwang.name>
 */
public class TwitterNotifyServlet extends HttpServlet 
{
	private static final long serialVersionUID = 7716647511499049357L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		List<TwitterNotify> tnList = TwitterNotifyDao.getInstance().getTwitterNotify();
		if(tnList == null || tnList.isEmpty())
		{
			return;
		}
		StringBuffer userIdSb = new StringBuffer();
		TwitterNotify tmp = null;
		boolean flag = false;   //标识是否有更新
		for(TwitterNotify tn : tnList)
		{
			if(!RobotMsg.getInstance().isOnline(tn.getUserJID()))
			{
				continue;
			}
			List<Status> statusList = new TwitterAPI().
				getHomeTimeline(tn.getUserName(), 
				DesEncrypt.getDesString(tn.getUserPwd()));
			if(statusList == null || statusList.isEmpty()
				|| statusList.get(0).getId() <= tn.getLastNotifyMsgId())
			{
				continue;
			}
			boolean isForGTalk = false;
			if(tn.getUserJID().toLowerCase().indexOf("talk.v") > 0)
			{
				isForGTalk = true;
			}
			String result = RobotMsg.getInstance().getTweetListStr(statusList, 
					tn.getLastNotifyMsgId(), isForGTalk);
			if(result.isEmpty())
			{
				continue;
			}
			flag = true;
			RobotMsg.getInstance().sendMessage(tn.getUserJID(), result);
			
			//更新用户信息
			tmp = TwitterNotifyDao.getInstance().getByUserId(tn.getUserId());
			tmp.setLastNotifyMsgId(statusList.get(0).getId());
			tmp.setLastNotifyTime(DateHelp.getLocalDateTime());
			tmp.setNotifyCount(tmp.getNotifyCount() + 1);
			TwitterNotifyDao.getInstance().addOrUpdateTwitterNotify(tmp);
			
			userIdSb.append(tn.getUserId());
			userIdSb.append(",");
		}
		if(!flag)
			return;
		
		userIdSb.append("Twitter消息提示");
		//写系统日志
		SysLog log = new SysLog();
		log.setUserId("system");
		log.setLogTime(DateHelp.getLocalDateTime());
		log.setLogType(SysLogType.doTwitterNotify);
		log.setLogContent(new Text(userIdSb.toString()));
		SysLogDao.getInstance().addSysLog(log);
	}
}
